/****************************************************************
*****************************************************************/

*by Xiaodong Fan, fanxiaodong@gmail.com

#delimit ;

cap log close;
clear all;
drop _all;
set more 1;
pause on;

log using log_MSM_cf_budget.log, replace;

if (strpos("`c(pwd)'", "_pthealth") > 0) {;
    global gpth = 1;
    global gpicsdir "pics_pthealth_cf";
};
else {;
    global gpth = 0;
    global gpicsdir "pics_cf";
};
global gAIMEn 28; // 28 is always lambda = 0.45;
global gAIMEnVary VaryPIA${gAIMEn}r;

global gdtadir ".";

include do_globals.do;

local lvbudgeta b_output b_sstax b_medicaretax b_finctax b_ssb b_ssdiinc b_ssdeficit b_ssdeficit_ssdi wage;

*plot now;
use ${gdtadir}/dta_MSM_cf_budget.dta, clear;

gen b_ssdeficit = b_ssb - b_sstax - b_medicaretax;
gen b_ssdeficit_ssdi = b_ssdiinc + b_ssb - b_sstax - b_medicaretax;

* generate tables, aggregate over individuals and ages;
local lvxsd "";
local lvxspd "";
foreach iv in `lvbudgeta' {;
   local lvxs_d `lvxsd' d_`iv';
   local lvxsd `lvxs_d';
   local lvxs_pd `lvxspd' pd_`iv';
   local lvxspd `lvxs_pd';
};

local lvxall `lvbudgeta' `lvxsd' `lvxspd';

order cfid `lvbudgeta';
sort cfid;

* translate into $thousand, *2000 / 1000;
foreach iv in b_output b_sstax b_medicaretax b_finctax b_ssb b_ssdiinc b_ssdeficit b_ssdeficit_ssdi {;
   replace `iv' = `iv' * 2;
};

* level;
preserve;
   xpose, clear varname;
   gen itype = "mean";
   save ${gpicsdir}/dta_MSM_cf_budget_table.dta, replace;
restore;

* difference;
preserve;
   foreach iv of local lvbudgeta {;
      gen `iv'_b = `iv' if cfid == 0;
      egen `iv'_base = min(`iv'_b);
      drop `iv'_b;
      replace `iv' = `iv'-`iv'_base;
      drop `iv'_base;
   };
   order cfid `lvbudgeta';
   sort cfid;
   xpose, clear varname;
   gen itype = "mean.d";
   append using ${gpicsdir}/dta_MSM_cf_budget_table.dta;
   save ${gpicsdir}/dta_MSM_cf_budget_table.dta, replace;
restore;
* percent difference;
preserve;
  foreach iv of local lvbudgeta {;
      gen `iv'_b = `iv' if cfid == 0;
      egen `iv'_base = min(`iv'_b);
      drop `iv'_b;
      replace `iv' = 100 * (`iv'-`iv'_base) / `iv'_base;
      drop `iv'_base;
   };
   order cfid `lvbudgeta';
   sort cfid;
   xpose, clear varname;
   gen itype = "mean.pd";
   append using ${gpicsdir}/dta_MSM_cf_budget_table.dta;
   save ${gpicsdir}/dta_MSM_cf_budget_table.dta, replace;
restore;

use ${gpicsdir}/dta_MSM_cf_budget_table.dta, clear;

ren v1 baseline;
ren v2 NoSSTest;
ren v3 DelayNRAonly;  //2--Delaying Social Security Normal Retirement Age (NRA);
ren v4 NoSSsystem;
ren v5 NoUncertainty;
ren v6 ReduceSSbenefit;
ren v7 NoSSbenefit;
ren v8 NoSStax;
ren v9 IncreaseTax;
ren v10 DelayNRAnUtil;  //9--Delaying NRA to 67 and change SSA utility accordingly;
if (${gpth}==1) {;
    ren v11 NoSS_wSSDI;
    ren v12 ReduceSSb_wSSDI;
    ren v13 NoSSb_wSSDI;
    local lvexp0 ReduceSSb_wSSDI NoSSb_wSSDI NoSS_wSSDI;
    local lvcn = 14;
};
else {;
    local lvexp0 "";
    local lvcn = 11;
};

ren v`lvcn' VaryPIA061r;
local lvcn0 = `lvcn'+1;
local lvcn = `lvcn0';
ren v`lvcn' VaryPIA01r;
local lvcn0 = `lvcn'+1;
local lvcn = `lvcn0';
ren v`lvcn' VaryPIA03r;
local lvcn0 = `lvcn'+1;
local lvcn = `lvcn0';
ren v`lvcn' VaryPIA05r;
local lvcn0 = `lvcn'+1;
local lvcn = `lvcn0';
ren v`lvcn' VaryPIA07r;
local lvcn0 = `lvcn'+1;
local lvcn = `lvcn0';
ren v`lvcn' VaryPIA09r;
local lvcn0 = `lvcn'+1;
local lvcn = `lvcn0';
ren v`lvcn' VaryAIMElast;
local lvcn0 = `lvcn'+1;
local lvcn = `lvcn0';
ren v`lvcn' VaryAIMElastMax;
local lvcn0 = `lvcn'+1;
local lvcn = `lvcn0';

local lvVarys "";
local lvVarys2 "";
local lvVarys3 "";
local lvVarys30 "";
forvalues iv = 28/28 {;
   ren v`lvcn' VaryPIA`iv'r;
   local lvcn0 = `lvcn'+1;
   local lvcn = `lvcn0';
   local lvVarys_0 `lvVarys' VaryPIA`iv'r;
   local lvVarys `lvVarys_0';
   local lvVarys2_0 `lvVarys2' VaryPIA`iv'r2;
   local lvVarys2 `lvVarys2_0';
   local lvVarys3_0 `lvVarys3' VaryPIA`iv'r3;
   local lvVarys3 `lvVarys3_0';
   local lvVarys30_0 `lvVarys30' VaryPIA`iv'r30;
   local lvVarys30 `lvVarys30_0';
};

local lvexp IncreaseTax NoSSTest DelayNRAonly DelayNRAnUtil ReduceSSbenefit 
            NoSSbenefit NoSStax NoSSsystem NoUncertainty `lvexp0'
            VaryPIA01r VaryPIA03r VaryPIA05r VaryPIA07r VaryPIA09r 
            VaryAIMElast 
            VaryPIA061r VaryAIMElastMax `lvVarys';

order itype _varname baseline `lvexp';

compress;
save ${gpicsdir}/dta_MSM_cf_budget_table.dta, replace;

**************************************************;
* make table directly for the paper;
*drop if trim(itype) == "mean" | trim(itype) == "mean.d" | trim(itype) == "mean.pd";

gen iid = 10 if trim(itype) == "mean";
replace iid = 20 if trim(itype) == "mean.d";
replace iid = 30 if trim(itype) == "mean.pd";

reshape wide itype baseline `lvexp', i(_varname) j(iid);

gen ivar = 10 if trim(_varname) == "b_output";
replace ivar = 20 if trim(_varname) == "b_sstax";
replace ivar = 30 if trim(_varname) == "b_medicaretax";
replace ivar = 40 if trim(_varname) == "b_ssb";
replace ivar = 60 if trim(_varname) == "b_ssdeficit";
replace ivar = 70 if trim(_varname) == "b_finctax";
replace ivar = 72 if trim(_varname) == "b_ssdiinc";

replace ivar = 75 if trim(_varname) == "b_ssdeficit_ssdi";
replace ivar = 80 if trim(_varname) == "wage";
replace ivar = 0 if trim(_varname) == "cfid";
sort ivar;

local lvextra "";
if (${gpth}==1) {;
    local lvextra23 ReduceSSb_wSSDI2 ReduceSSb_wSSDI3 NoSSb_wSSDI2 NoSSb_wSSDI3 NoSS_wSSDI2 NoSS_wSSDI3;
    local lvextra2030 ReduceSSb_wSSDI20 ReduceSSb_wSSDI30 NoSSb_wSSDI20 NoSSb_wSSDI30 NoSS_wSSDI20 NoSS_wSSDI30;
};
else {;
    local lvextra23 "";
    local lvextra2030 "";
    *local lvextra NoUncertainty2 NoUncertainty3;
};
*local lvextra NoUncertainty12 NoUncertainty13;
if (${gpth}==1) {;
    export excel _varname baseline10 IncreaseTax30 NoSSTest30 DelayNRAnUtil30
          ReduceSSb_wSSDI30 ${gAIMEnVary}30 VaryAIMElast30 
          NoSStax30 NoSSb_wSSDI30 NoSS_wSSDI30
       using "table4paper${alt_name}", sheet("cf_mean${alt_name}_budget") 
       sheetreplace firstrow(variables);
    export excel _varname baseline10 VaryPIA01r30 
            VaryPIA03r30 VaryPIA05r30 VaryPIA07r30 VaryPIA09r30
       using "table4paper${alt_name}", sheet("cf_mean${alt_name}_budget_AIME") 
            sheetreplace firstrow(variables);
};
else {;
     export excel _varname baseline10 IncreaseTax30 NoSSTest30 DelayNRAnUtil30
          ReduceSSbenefit30 ${gAIMEnVary}30 VaryAIMElast30
          NoSStax30 NoSSbenefit30 NoSSsystem30
       using "table4paper${alt_name}", sheet("cf_mean${alt_name}_budget") 
          sheetreplace firstrow(variables);
     export excel _varname baseline10 VaryPIA01r30 
            VaryPIA03r30 VaryPIA05r30 VaryPIA07r30 VaryPIA09r30
       using "table4paper${alt_name}", sheet("cf_mean${alt_name}_budget_AIME") 
            sheetreplace firstrow(variables);   
};

export excel _varname baseline10 `lvVarys30' 
       using "table4paper${alt_name}", sheet("cf_mean${alt_name}_budget_AIME2") 
            sheetreplace firstrow(variables);

cap log close;


